﻿package com.tvein.sanguo.swc.tool
{
	//flash
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter; 
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	//A3D
	//custom
	import com.tvein.sanguo.swc.common.SwcEvent;
	import com.tvein.sanguo.swc.common.MP3Click;
//
	public class ToolShell extends MovieClip
	{
		public static const NAME:String = "ToolShell";
		
		private var effectVol:int = 100;
		
		public function ToolShell()
		{
			stage?init(null):addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE , init);
			addEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.addEventListener(Event.RESIZE, _resizeUI);
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			btnMission.addEventListener(MouseEvent.CLICK, onClick);
			btnMail.addEventListener(MouseEvent.CLICK, onClick);
			btnAdvice.addEventListener(MouseEvent.CLICK, onClick);
			btnGold.addEventListener(MouseEvent.CLICK, onClick);
			btnFunction.addEventListener(MouseEvent.CLICK, onClick);
			
			_resizeUI();
		}
		
		public function console(Type:String, params:Object):void
		{
			switch (Type)
			{
				case "setEffect":
					effectVol = params.Volume;
					break;
				case "setShine":
					setButtonShine(params[0], params[1]);
					break;
			}
		}
		
		private function setButtonShine(buttonName:String, type:Boolean):void
		{
			var glow:GlowFilter = new GlowFilter(0x0000FF);
			
			switch (buttonName)
			{
				case "mission":
					if (type)
					{
						btnMission.filters = [glow];
					}
					else
					{
						btnMission.filters = [];
					}
					break;
				case "mail":
					if (type)
					{
						btnMail.filters = [glow];
					}
					else
					{
						btnMail.filters = [];
					}
					break;
				case "advice":
					if (type)
					{
						btnAdvice.filters = [glow];
					}
					else
					{
						btnAdvice.filters = [];
					}
					break;
				case "gold":
					if (type)
					{
						btnGold.filters = [glow];
					}
					else
					{
						btnGold.filters = [];
					}
					break;
				case "function":
					if (type)
					{
						btnFunction.filters = [glow];
					}
					else
					{
						btnFunction.filters = [];
					}
					break;
			}
		}
		
		private function _onRemoved(evt:Event):void
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.removeEventListener(Event.RESIZE, _resizeUI);
			
			btnMission.removeEventListener(MouseEvent.CLICK, onClick);
			btnMail.removeEventListener(MouseEvent.CLICK, onClick);
			btnAdvice.removeEventListener(MouseEvent.CLICK, onClick);
			btnGold.removeEventListener(MouseEvent.CLICK, onClick);
			btnFunction.removeEventListener(MouseEvent.CLICK, onClick);
		}
		
		private function _resizeUI(evt:Event = null):void
		{
			this.x = (stage.stageWidth - this.width) - 10;
			this.y = 10;
		}
		
		private function onClick(evt:MouseEvent):void
		{
			var mp3Click:MP3Click = new MP3Click();
			mp3Click.playback(effectVol * .01);
			
			switch(evt.target.name)
			{
				case "btnMission":
					dispatchEvent(new SwcEvent(NAME, ["-tool", ["mission"]]));
					setButtonShine("mission", false);
					break;
				case "btnMail":
					dispatchEvent(new SwcEvent(NAME, ["-tool", ["mail"]]));
					setButtonShine("mail", false);
					break;
				case "btnAdvice":
					dispatchEvent(new SwcEvent(NAME, ["-tool", ["advice"]]));
					setButtonShine("advice", false);
					break;
				case "btnGold":
					dispatchEvent(new SwcEvent(NAME, ["-tool", ["gold"]]));
					break;
				case "btnFunction":
					dispatchEvent(new SwcEvent(NAME, ["-menu", ["function"]]));
					break;
			}
		}
	}
}